package com.github.shiqiyue.learn.guava;

import com.google.common.math.BigIntegerMath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;
import java.math.RoundingMode;

/***
 * 描述
 * @author wenyao.wu
 * @date 2018/7/4
 */
public class BigIntegerMathTest {

    private static final Logger logger = LoggerFactory.getLogger(BigIntegerMathTest.class);


    /***
     * 大整数操作
     * @param args
     */
    public static void main(String[] args) {
        /***
         * 大整数整除，
         * RoundingMode.UNNECESSARY : 断言请求的操作具有精确的结果，因此不需要舍入。
         * 如果对获得精确结果的操作指定此舍入模式，则抛出ArithmeticException。
         *
         */
        logger.info("{}", BigIntegerMath.divide(BigInteger.TEN, new BigInteger("2"), RoundingMode.UNNECESSARY));
        try {
            logger.info("{}", BigIntegerMath.divide(BigInteger.TEN, new BigInteger("3"), RoundingMode.UNNECESSARY));
        } catch (ArithmeticException e) {
            logger.error("Error:{} ", e);
        }

        /***
         * log2
         */
        logger.info("Log2(2): " + BigIntegerMath.log2(new BigInteger("2"), RoundingMode.HALF_EVEN));

        /**
         * lg
         */
        logger.info("Log10(10): " + BigIntegerMath.log10(BigInteger.TEN, RoundingMode.HALF_EVEN));

        /***
         * sqrt
         */
        logger.info("sqrt(100): " + BigIntegerMath.sqrt(BigInteger.TEN.multiply(BigInteger.TEN), RoundingMode.HALF_EVEN));
        /***
         * 阶乘
         */
        logger.info("factorial(5): " + BigIntegerMath.factorial(5));
    }
}
